package cn.com.scitc.soft2101.servlet.paid;

import cn.com.scitc.soft2101.dao.PaidDao;
import cn.com.scitc.soft2101.model.Paid;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

// 动作：插入
@WebServlet(urlPatterns = "/paid/create")
public class PaidCreateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String paidName = req.getParameter("paidName");
            String paidType = req.getParameter("paidType");

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
            Date paidDate = sdf.parse(req.getParameter("paidDate"));

            BigDecimal amountPaid = new BigDecimal(req.getParameter("amountPaid"));
            String remark = req.getParameter("remark");

            System.out.println("paidName: " + paidName + "; paidType: " + paidType + "; paidDate: " + paidDate + "; amountPaid: " + amountPaid + "; remark: " + remark);

            Paid paid = new Paid();
            paid.setPaidName(paidName);
            paid.setPaidType(paidType);
            paid.setPaidDate(paidDate);
            paid.setAmountPaid(amountPaid);
            paid.setRemark(remark);
            PaidDao paidDao = new PaidDao();
            paidDao.insert(paid);

            System.out.println("insert success");
            req.getSession().setAttribute("msg", "新增成功");
            resp.sendRedirect("list");
        }
        catch (ParseException er) {
            er.printStackTrace();
        }
    }
}
